package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * SysUserController
 *
 * @author LILIBO
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Resource
    SysUserService sysUserService;

    @PostMapping("/login")
    public String login(Model model, HttpSession session, @RequestParam String account, @RequestParam String password) {
        System.out.println("account: " + account + " password: " + password);
        SysUser sysUser = sysUserService.getSysUserByAccount(account, password);
        if (sysUser == null) {
            model.addAttribute("error", "账号或密码错误");
            return "login"; // 登录失败，跳转回登录页面
        }
        // 数据库账号密码匹配成功之后，将用户对象保存到Session作用域中
        session.setAttribute("USER_SESSION", sysUser);
        return "frame"; // 登录成功，跳转到后台页面
    }
}
